package cn.pfund.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.pfund.controller.BaseController;
import cn.pfund.entity.News;
import cn.pfund.entity.NewsType;
import cn.pfund.service.NewsService;
import cn.pfund.service.NewsTypeService;
import cn.pfund.util.Format;
import cn.pfund.util.Pagination;
import cn.pfund.util.ParamBean;

/**
 * 新闻管理
 * @author zhaominglei qq:351024274，专注于网站、app和游戏开发。
 * @date 2017年1月20日
 */
@Controller("adminNewsController")
@RequestMapping("/admin/news")
public class NewsController extends BaseController {
    @Resource(name="newsService")
    private NewsService newsService;
    @Resource(name="newsTypeService")
    private NewsTypeService newsTypeService;
    
    @RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/news/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<News> pagination = newsService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/news/list";
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("newsTypes", newsTypeService.getAllNewsType());
		return "admin/news/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(News news, HttpServletRequest request, HttpServletResponse response, Model model){
		String newsTypeId = request.getParameter("newsTypeId");
		if (newsTypeId == null || newsTypeId.equals("")) {
			return ERROR_VIEW;
		}
		NewsType newsType = newsTypeService.getById(NewsType.class, Long.valueOf(newsTypeId));
		if (newsType == null) {
			return ERROR_VIEW;
		}
		news.setNewsType(newsType);
		newsService.build(news);
		newsService.save(news);
		return "redirect:/admin/news/list.shtml";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("news", newsService.getById(News.class, id));
		request.setAttribute("newsTypes", newsTypeService.getAllNewsType());
		return "admin/news/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(News news, HttpServletRequest request, HttpServletResponse response, Model model){
		News pNews = newsService.getById(News.class, news.getId());
		if (pNews == null) {
			return ERROR_VIEW;
		}
		String newsTypeId = request.getParameter("newsTypeId");
		if (newsTypeId == null || newsTypeId.equals("")) {
			return ERROR_VIEW;
		}
		NewsType newsType = newsTypeService.getById(NewsType.class, Long.valueOf(newsTypeId));
		if (newsType == null) {
			return ERROR_VIEW;
		}
		news.setNewsType(newsType);
		newsService.build(news);
		news.setSource((news.getSource() == null || news.getSource().equals("") ? pNews.getSource() : news.getSource()));
		newsService.update(news);
		return "redirect:/admin/news/list.shtml";
	}
	
	@RequestMapping(value = "/view",method=RequestMethod.GET)
	public String view(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("news", newsService.getById(News.class, id));
		return "admin/news/view";
	}

    @RequestMapping(value = "/delete",method=RequestMethod.GET)
    public String delete(String ids, HttpServletRequest request, HttpServletResponse response, Model model) {
        String[] idArray = ids.split(","); //支持批删 id逗号隔开
        for(String id:idArray){
        	News news = newsService.getById(News.class, Long.valueOf(id));
        	if (news == null) continue;
        	newsService.delete(news);
        }
        return "redirect:/admin/news/list.shtml";
    }	
}